import pygame


class Matrix(object):
    def __init__(self, row: int = 75, col: int = 100):
        self.row = row
        self.col = col
        self.stop = False
        self.matrix = []
        self.init()

    def init(self):
        self.top = False
        self.matrix = [[0 for _ in range(self.col)] for _ in range(self.row)]
        # for i in range(3):
        #     for j in range(3):
        #         self.matrix[30 + i][50 + j] = 1
        # 初始化位置
        matrix = [[0 for _ in range(5)] for _ in range(3)]
        for row in range(3):
            for col in range(5):
                matrix[row][col] = 0 if row in [0, 2] and col in [1, 2, 3] else 1
        for row in range(3):
            for col in range(5):
                self.matrix[row + 30][col + 50] = matrix[row][col]

    def get_nonzero_position(self):
        self.index = []
        for i, row in enumerate(self.matrix):
            for j, col in enumerate(row):
                if col != 0:
                    self.index.append([i, j])

    def update(self):
        exMatrix = self.matrix
        matrix = [[0 for _ in range(self.col)] for _ in range(self.row)]
        for row in range(1, self.row-1):
            for col in range(1, self.col-1):
                sum = self.matrix[row - 1][col - 1] + self.matrix[row - 1][col] + self.matrix[row - 1][col + 1] + \
                      self.matrix[row][col - 1] + self.matrix[row][col + 1] + self.matrix[row + 1][col - 1] + \
                      self.matrix[row + 1][col] + self.matrix[row + 1][col + 1]
                if sum in [0, 1, 4, 5, 6, 7, 8]:
                    matrix[row][col] = 0
                elif sum == 3:
                    matrix[row][col] = 1
                else:
                    matrix[row][col] = self.matrix[row][col]
        self.matrix = matrix
        if exMatrix == self.matrix:
            self.stop = True


class GameOfLife(object):
    def __init__(self, row: int = 75, col: int = 100, space: int = 1):
        self.size = 9
        self.space = space
        self.row = row
        self.col = col
        self.data = Matrix(row, col)
        self.goEnable = False
        self.speed = 10
        self.step = 0
        pygame.init()
        # 设置窗口屏幕尺寸
        self.screenHeight = self.boardHeight = row * (self.size + self.space)
        self.boardWidth = col * (self.size + self.space)
        self.screenWidth = self.boardWidth + 200
        self.screen = pygame.display.set_mode((self.screenWidth, self.boardHeight))
        # 设置窗口标题文本
        pygame.display.set_caption("生命游戏")
        self.init_screen()
        self.update_GUI()
        self.clock = pygame.time.Clock()
        self.run()

    def run(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                if event.type == pygame.KEYDOWN:
                    # 空格事件
                    if event.key == pygame.K_SPACE:
                        self.goEnable = not self.goEnable
                    # elif event.key == pygame.KSCAN_KP_ENTER:
                    #     print("reset")
                    #     self.reset_game()
                        # 每秒刷新10帧
            self.clock.tick(self.speed)
            if self.goEnable:
                self.step += 1
                print(self.step)
                self.data.update()
                self.update_GUI()
            if self.data.stop:
                self.goEnable = False
    def reset_game(self):
        self.data.init()
        self.init_screen()


    def update_GUI(self):
        for i in range(self.row):
            for j in range(self.col):
                cell = pygame.Rect(j * (self.space + self.size) + self.space, i * (self.space + self.size) + self.space,
                                   self.size, self.size)
                pygame.draw.rect(self.screen, (0, 0, 0) if self.data.matrix[i][j] == 1 else (255, 255, 255), cell)
        pygame.display.update()

    def init_screen(self):
        self.screen.fill((255, 255, 255))
        # 将屏幕填充白色，并绘制纵横灰色线段，
        for i in range(self.row):
            pygame.draw.line(self.screen, (0xd0, 0xd0, 0xd0) if i % 5 == 0 else (0xe5, 0xe5, 0xe5), (0, i * 10),
                             (self.boardWidth, i * 10))  # 画横线
        for i in range(self.col):
            pygame.draw.line(self.screen, (0xd0, 0xd0, 0xd0) if i % 5 == 0 else (0xe5, 0xe5, 0xe5), (i * 10, 0),
                             (i * 10, self.boardHeight))  # 画竖线
        pygame.display.update()


if __name__ == '__main__':
    gameOfLife = GameOfLife(33,55,1)
